<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* Control panel class
	Author 	: Iswan Putera S.Kom
	Version	: 2.0
	Date	: January 2013
*/
class Admin extends CI_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->load->model('main_model');
		$this->load->library('zetro_auth');
		//force_ssl();

	}
	public function index($data='')
	{
	  if($this->session->userdata('login')!=TRUE){
		$this->load->view('admin/header',$data);
		$this->load->view('admin/main');
		$this->load->view('admin/footer');
		$this->zetro_auth->_generate_user_table();
	  }else{
		  $this->home();
	  }
	}
	function home()
	{
		$data=array();
		$data['menu']='home';
		$this->zetro_auth->check_login();
		$this->zetro_auth->menus('home');
		$this->zetro_auth->noAuth();
		$this->session->unset_userdata('menus');
		$this->load->view('admin/header');
		$this->load->view('admin/main');
		$this->load->view('admin/footer',$data);
	}
	
	function user_admin()
	{
		//tab header
		$this->zetro_auth->check_login();
		$this->zetro_form->frm_file('asset/frm/zetro_menu.frm');
		$this->zetro_form->section('User Account');
		$data['tab_header']=$this->zetro_form->tab_header();
		//tab1 content
		$this->zetro_form->frm_file('asset/frm/zetro_user.frm');
		$this->zetro_form->section('newUser');
		$this->main_model->table_name('user_group');
		$this->main_model->where("nm_group !='Superuser'");
		$this->main_model->dropdwn_select();
		$this->zetro_form->get_select_opt($this->main_model->dropdown_data('nm_group','nm_group'));
		$this->zetro_auth->menus('add_user');
		$this->zetro_form->addButtons();
		$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?true:false);
		$data['modal']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm2'),"Edit User Account");
		$data['form']=$this->zetro_form->frm_std();
		//tab2 content
		$this->zetro_form->section('UserList');
		$this->zetro_form->action_col(true);
		$content='';$i=0;$lst_usr=array();
		$this->main_model->table_name('users');
		$this->main_model->query('');
		$lst_usr=$this->main_model->show_users('nm_group','Superuser');
		foreach($lst_usr as $r)
		{
			$i++;
			$this->zetro_auth->menus('list_user');
			$icon=($this->zetro_auth->cek_oto('e')!='')?
				  $this->zetro_form->z_icon($r->userid,array('cus-database-gear','cus-cross'),array('edit','deleted')):'';
			$content.=tr().td($i,false,'','','center').
					  td($r->userid).td($r->username).td($r->nm_group).
					  td($icon,false,'','','center')._tr();
		}
		$content.=$this->zetro_form->DataNotFound($lst_usr,4);
		$this->zetro_form->tabel_content($content);
		$data['list']=$this->zetro_form->list_std('NewList','70%');
		//tab3 content
		$this->zetro_form->section('UserOto');
		$this->zetro_form->buttons();
		$this->main_model->table_name('user_group');
		$this->main_model->where("nm_group !='Superuser'");
		$this->main_model->dropdwn_select($this->session->userdata('user_group'));
		$this->zetro_form->get_select_opt($this->main_model->dropdown_data('nm_group','nm_group'));
		$this->zetro_form->buttons(false);
		$data['grpoto']=$this->zetro_form->frm_std();
		//list oto
		$content='';//$this->User_Oto();
		$this->zetro_form->tabel_content($content);
		$this->zetro_form->section('UserOtoLst');
		$this->zetro_form->action_col();
		$data['listOto']=$this->zetro_form->list_std('zOto','60%');
		$data['lok']=$this->zetro_form->generate_dropdown_content('lokasi','id_lokasi','nm_lokasi');
		//list modul
		$this->zetro_form->frm_file('asset/frm/zetro_menu.frm');
		$this->zetro_form->section('menudashboard');
		$data['module']=$this->zetro_form->nav_pill();
		//send to view
		$this->load->view('admin/header');
		$this->load->view('admin/user_admin',$data);
		$this->load->view('admin/footer');
	}
	
	//set otorisasi
	function set_otorisasi()
	{
		$field=$_POST['field'];
		$menu=$_POST['menu'];
		$usr_grp=$_POST['nm_grp'];
		$stat=empty($_POST['stat'])?'N':$_POST['stat'];
		$this->main_model->table_name('user_oto');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('userid'=>$usr_grp,'modul'=>$menu));
		if($this->main_model->set_data()){
			$this->main_model->set_methods('update');
			$this->main_model->get_content(array($field=>$stat));
			$this->main_model->where(array('userid'=>$usr_grp,'modul'=>$menu));
			$this->main_model->set_data();
		}else{
			$this->main_model->set_methods('insert');
			$this->main_model->get_content(array($field=>$stat,'userid'=>$usr_grp,'modul'=>$menu));
			$this->main_model->set_data();
		}
	}
	//tampilkan data otorisasi
	function User_Oto()
	{
		$id=empty($_POST['id'])?'':str_replace('_',' ',$_POST['id']);
		$grp=empty($_POST['nm_grp'])?$this->session->userdata('user_group'):$_POST['nm_grp'];
		$this->zetro_form->active_user($grp);
		$datax=$this->zetro_form->list_menu($id);
		echo $datax;	
	}
	
	//simpan data user group
	function set_usergroup()
	{
		$data=array();
		$data['nm_group']=($_POST['name']);
		$this->main_model->table_name('user_group');
		$this->main_model->set_methods('replace');
		$this->main_model->get_content($data);
		echo ($this->main_model->set_data())? $this->main_model->dropdown_data('nm_group','nm_group'):'';
		;
		
	}
	
	//simpan data userid
	function set_user()
	{
		$data=array();
		$data['userid']=$_POST['userid'];
		$data['username']=($_POST['username']);
		$data['password']=md5($_POST['pwd']);
		$data['nm_group']=empty($_POST['usergrp'])?'Guest':$_POST['usergrp'];
		$data['app_document']=empty($_POST['app_doc'])?'N':$_POST['app_doc'];
		$data['id_lokasi']=empty($_POST['id_lokasi'])?'0000':$_POST['id_lokasi'];
		$data['useremail']=empty($_POST['useremail'])?'':$_POST['useremail'];
		$this->main_model->prepared_data('users',$data);
		echo success($this->main_model->set_data());
		
	}
	
	//tampilkan detail user
	function get_user_detail()
	{
		$userid=$_POST['userid'];
		$this->main_model->table_name('users');
		$this->main_model->set_methods('select_detail');
		$this->main_model->query();
		$this->main_model->where(array('userid'=>$userid));
		echo json_encode($this->main_model->set_data());	
	}
	
	//update data user
	function update_user()
	{
		$data['userid']=$_POST['userid'];
		$data['username']=($_POST['username']);
		$data['nm_group']=empty($_POST['usergrp'])?'Guest':$_POST['usergrp'];
		$data['app_document']=empty($_POST['app_doc'])?'N':$_POST['app_doc'];
		$data['useremail']=empty($_POST['emailuser'])?'':$_POST['emailuser'];
		$data['id_lokasi']=empty($_POST['id_lokasi'])?'0000':$_POST['id_lokasi'];
		$this->main_model->prepared_data('users',$data,'update');
		$this->main_model->where(array('userid'=>$_POST['userid']));
		$this->main_model->set_data();	
	}
	//hapus userid from database
	function deleted_user()
	{
		$userid=$_POST['userid'];
		$this->main_model->table_name('users');
		$this->main_model->set_methods('delete');
		$this->main_model->where(array('userid'=>$userid));
		$this->main_model->set_data();	
	}
	
	// proces login
	function login_process()
	{
		$userid	=$_POST['userid'];
		$pwd	   =md5($_POST['password']);
		$userid	=$_POST['userid'];
		$this->main_model->table_name('users');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('userid'=>$userid,'password'=>$pwd));
		$data=$this->main_model->set_data();
		if($data){
			foreach($data as $r)
			{
				$ses_data=array('userid'	=>$r->userid,
								'username'	=>$r->username,
								'user_group'=>$r->nm_group,
								'approval'=>$r->app_document,
								'login'		=>TRUE);	
			}
			$this->session->set_userdata($ses_data);
			//$this->home();
			$this->zetro_auth->user_log();
			redirect('admin/home');
		}else{
			$data['message']="<i class='cus-exclamation'></i> Userid or password incorrect!!";
			$this->index($data);
		}
	}
	function masuk()
	{
		$id=$_GET['id'];
		$data['menu']=base64_decode($id);
		$data['judul']=base64_decode($id);
		$sesmenu=array('menus'=>$_GET['id']);
		$this->session->set_userdata($sesmenu);
		$this->zetro_auth->user_log(base64_decode($id));
		$this->load->view('admin/header',$data);
		$this->load->view(str_replace(" ","",strtolower(base64_decode($id))).'/home',$data);
		$this->load->view('admin/footer',$data);
			
	}
	function log_out()
	{
		$this->zetro_auth->log_out();
	}	
	//change password
	function change_password()
	{
		$data=array();
		//menu header
		$this->zetro_form->frm_file('asset/frm/zetro_menu.frm');
		$this->zetro_form->section('Change Password');
		$data['tab_header']=$this->zetro_form->tab_header();
		//generate form
		$this->zetro_form->frm_file('asset/frm/zetro_user.frm');
		$this->zetro_form->section('changepwd');
		$this->zetro_auth->menus('input_data');
		$this->zetro_form->buttons(true);
		$data['form']=$this->zetro_form->frm_std();
		//send data to view
		$this->load->view('admin/header');
		$this->load->view('admin/change_pwd',$data);
		$this->load->view('admin/footer');
	}
	function get_old_pwd()
	{
		$this->main_model->query();
		$this->main_model->where(array('userid'=>$this->session->userdata('userid')));
		$this->main_model->table_name('v_users');
		$this->main_model->set_methods('select');
		$data=$this->main_model->set_data();
		foreach($data as $r){
			echo($r->password==md5($_POST['op']))?'1':'Old Password no match, please try again';
		}
	}
	
	function set_new_pwd()
	{
		$data['userid']=$this->session->userdata('userid');
		$data['password']=md5($_POST['np']);
		$this->main_model->prepared_data('users',$data,'update');
		$this->main_model->where(array('userid'=>$this->session->userdata('userid')));
		echo success($this->main_model->set_data());	
		
	}
	
	//document approval
	//setting approver level untuk document / modul yang perlu dilakukan approval
	function app_doc()
	{
		$data=array();
		//menu header
		$data['tab_header']=$this->zetro_form->generate_tab_header('Document Approver');
		//generate form
		$this->zetro_form->frm_file('asset/frm/zetro_user.frm');
		$this->zetro_form->section('applevel');
		$this->zetro_form->generate_dropdown_content('v_users','userid','username',array('app_document'=>'Y'),
		$this->session->userdata('userid'),'');
		$this->zetro_auth->menus('document_approver');
		$this->zetro_form->buttons(true);
		//$data['form']=$this->zetro_form->frm_std();
		$data['modal']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm1','60%'),"Add New Approver");
		//send data to view
		$content='';//$this->get_list_lokasi();
		$this->zetro_form->section('listdocapp');
		$this->zetro_form->action_col(true);
		$content.="";//$this->zetro_form->DataNotFound($lst_usr,4);
		$this->zetro_form->tabel_content($content);
		$data['list']=$this->zetro_form->list_std();
		$this->load->view('admin/header');
		$this->load->view('admin/approved',$data);
		$this->load->view('admin/footer');
		
	}
	function set_app_doc()
	{
		$data=array();
		$data['id_doc']=empty($_POST['id_doc'])?'':$_POST['id_doc'];
		$data['nm_doc']=empty($_POST['nm_doc'])?'':$_POST['nm_doc'];
		$data['app_level']=empty($_POST['app_doc'])?'':$_POST['app_doc'];
		$data['level_1']=empty($_POST['level_1'])?'':$_POST['level_1'];
		$data['level_2']=empty($_POST['level_2'])?'':$_POST['level_2'];
		$data['level_3']=empty($_POST['level_3'])?'':$_POST['level_3'];
		$data['level_4']=empty($_POST['level_4'])?'':$_POST['level_4'];
		$this->main_model->prepared_data('user_approver',$data);
		echo success($this->main_model->set_data());
		
	}
	
	function get_app_doc_detail()
	{
		$data=array();
		$id=$_POST['id'];
		$this->main_model->table_name('user_approver');
		$this->main_model->set_methods('select');
		$this->main_model->query();
		$this->main_model->where(array('id_doc'=>$id));
		$data=$this->main_model->set_data();
		echo json_encode($data[0]);	
	}
	
	function get_app_doc_list()
	{
		$content='';$i=0;$lst_usr=array();
		$tp=empty($_POST['tp'])?'':$_POST['tp'];
		$ordby=empty($_POST['ordby'])?'id_doc':$_POST['ordby'];
		$this->main_model->table_name('v_app_doc');
		$this->main_model->query();
		$this->main_model->orderby($ordby);
		$this->main_model->set_methods('select_ordered');
		$lst_usr=$this->main_model->set_data();
		foreach($lst_usr as $r)
		{
			$i++;
			$this->zetro_auth->menus('document_approver');
			$icon=($this->zetro_auth->cek_oto('e')!='')?
				  $this->zetro_form->z_icon($r->id_doc,array('cus-page-white-edit','cus-cross'),array('edit','deleted')):'';
			$content.=tr().td($i,false,'','','center').
					  td(strtoupper(str_replace('_',' ',$r->id_doc)),'',false,'','left\' nowrap=\'nowrap').td($r->nm_doc).
					  td($r->app_level,false,'','','center').
					  td($r->level_1).td($r->level_2).
					  td($r->level_3).td($r->level_4).
					  td($icon,false,'','','center')._tr();
		}
			$content.=$this->zetro_form->DataNotFound($lst_usr,9);
			echo $content;
	}
	
	function del_app_doc()
	{
		$id=$_POST['id'];
		$this->main_model->table_name('user_approver');
		$this->main_model->set_methods('delete');
		$this->main_model->where(array('id_doc'=>$id));
		$this->main_model->set_data();	
	}
	//setting modul yang membutuhkan approval untuk bisa di proses selanjutnya
	function app_mod()
	{
		$data=array();
		//menu header
		$data['tab_header']=$this->zetro_form->generate_tab_header('Modul Approval Setting');
		//generate form
		$this->zetro_form->frm_file('asset/frm/zetro_master.frm');
		$this->zetro_auth->menus('modul_approval_setting');
		$content='';//$this->get_list_lokasi();
		$this->zetro_form->section('modulapp');
		$this->zetro_form->action_col(false);
		$content.="";//$this->zetro_form->DataNotFound($lst_usr,4);
		$this->zetro_form->tabel_content($content);
		$data['list']=$this->zetro_form->list_std();
		$this->load->view('admin/header');
		$this->load->view('administration/modul_approved',$data);
		$this->load->view('admin/footer');
	}
	function get_list_app_mod()
	{
		$this->zetro_form->active_user($this->session->userdata('userid'));
		echo $this->zetro_form->modul_app_seting();	
	}
	
	function set_app_modul()
	{
		$data=array();
		$data['nm_menu']=$_POST['menu'];
		$data['c']		=$_POST['stat'];
		$data['created_by']=$this->session->userdata('userid');
		$this->main_model->prepared_data('modul_approval',$data);
		echo success($this->main_model->set_data());
			
	}
	
	function get_modul_dropdown()
	{
		$this->main_model->table_name('v_modul_app');
		$this->main_model->query();
		$this->main_model->set_methods('select');
		$lst_usr=$this->main_model->set_data();
		$data="<option value=''></option>";
		foreach($lst_usr as $r)
		{
			$data.="<option value='".$r->nm_menu."'>".ucwords($r->menune)."</option>";
		}
		echo $data;
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */